package com.cooper.mediadb;

import java.util.ArrayList;
import java.util.List;

import com.cooper.scrapemedialib.R;
import com.cooper.scraper.show.AdapterDataChangedListener;
import com.cooper.scraper.show.FanartAdapter;
import com.cooper.scraper.show.FanartViewStrategy;
import com.cooper.scraper.show.FanartsShow;
import com.cooper.scraper.show.FlowGalleryViewFactory;
import com.cooper.scraper.show.ResourceCommandAction;
import com.cooper.scraper.show.ResourceShowListener;
import com.cooper.scraper.show.TileGalleryViewFactory;
import com.cooper.slidingmenu.utils.SampleDialogBaseFragment;
import com.cooper.slidingmenu.utils.SingleFragmentContain;

import android.net.Uri;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

public abstract class MediaBrowseBaseFragment<T extends Media> extends
		SampleDialogBaseFragment implements AdapterDataChangedListener,
		ResourceShowListener<T> {

	// public static final int AD_VIEW_HEIGHT = 76;
	private static final int GALLERY_HEIGHT = 440; // pix

	protected LinearLayout actionBarFrame;

	private RelativeLayout mainLayout;

	private View noResultMessage;
	private View rightWait;

	private boolean tileGallery;

	public MediaBrowseBaseFragment() {
		super();
		setRetainInstance(true);
	}

	protected FanartsShow<T> fanartsViewShow = null;

	protected T currentMedia;

	public T getCurrentMedia() {
		return currentMedia;
	}

	protected void switchMediaViewShow() {
		if (fanartsViewShow != null) {
			if ((tileGallery && fanartsViewShow instanceof TileGalleryViewFactory)
					|| (!tileGallery && fanartsViewShow instanceof FlowGalleryViewFactory))
				return;
			fanartsViewShow.removeFrom(mainLayout);
			fanartsViewShow.destroy();
		}
		if (tileGallery) {
			fanartsViewShow = new TileGalleryViewFactory<T>(getActivity()
					.getApplicationContext(), getViewStrategy(), getActivity()
					.getWindowManager().getDefaultDisplay().getWidth(),
					getGalleryViewHeight(), Media.HEIGHT_WIDHT_SCALE,
					R.drawable.missing);

		} else
			fanartsViewShow = new FlowGalleryViewFactory<T>(getActivity()
					.getApplicationContext(), getViewStrategy(), getActivity()
					.getWindowManager().getDefaultDisplay().getWidth(),
					getGalleryViewHeight(), Media.HEIGHT_WIDHT_SCALE,
					R.drawable.missing);

		FanartAdapter<T> adapter = getFanartAdapter();
		adapter.setDataChangedListener(this);

		fanartsViewShow.registerContextMenu(this);

		fanartsViewShow.setAdapter(adapter);

		fanartsViewShow.setCommands(commands);
		fanartsViewShow.showTo(mainLayout);

		fanartsViewShow.setMediasShowListener(this);
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Log.d("MediaBrowseBaseFragment", "Fragment call onCreate:" + this);
		setRetainInstance(true);
		initCommandAction();
		tileGallery = PreferenceManager.getDefaultSharedPreferences(
				getActivity()).getBoolean("perfernce_tile_fanart", false);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		Log.d("MediaBrowseBaseFragment", "Fragment call onCreateView:" + this);
		View result = inflater.inflate(getContentView(), container, false);
		findView(result);
		return result;
	}

	@Override
	public void onDestroyView() {
		super.onDestroyView();
		Log.d("MediaBrowseBaseFragment", "Fragment call onDestroyView:" + this);
		if (fanartsViewShow != null) {
			fanartsViewShow.destroy();
			fanartsViewShow = null;
		}
	}

	@Override
	public void dataWaiting() {
		if (noResultMessage != null)
			noResultMessage.setVisibility(View.GONE);
		if (rightWait != null) {
			rightWait.setVisibility(View.VISIBLE);
		}

	}

	@Override
	public void dataComplete() {
		if (rightWait != null) {
			rightWait.setVisibility(View.GONE);
		}
		// WaitNextDataListener.DIRECTION_DOWN
	}

	@Override
	public void noResultData() {
		if (noResultMessage != null)
			noResultMessage.setVisibility(View.VISIBLE);
	}

	protected abstract void showMovieDetails(T media);

	protected List<ResourceCommandAction<T>> commands;

	protected void initCommandAction() {
		commands = new ArrayList<ResourceCommandAction<T>>();
		ResourceCommandAction<T> actionCommand = new ResourceCommandAction<T>() {
			@Override
			public void execute(T media) {
				playMedia(media);
			}
		};
		actionCommand.setGroup(0);
		actionCommand.setIconResId(R.drawable.ic_play);
		actionCommand.setTitle(getResources().getString(
				R.string.media_menu_play));
		commands.add(actionCommand);

		actionCommand = new ResourceCommandAction<T>() {
			@Override
			public void execute(T media) {
				showMovieDetails(media);
			}
		};
		actionCommand.setGroup(0);
		actionCommand.setIconResId(R.drawable.ic_details);
		actionCommand.setTitle(getResources().getString(
				R.string.media_menu_info));
		commands.add(actionCommand);

	}

	protected void findView(View view) {
		mainLayout = (RelativeLayout) view.findViewById(R.id.main_layout);

		noResultMessage = view.findViewById(R.id.no_result_movie);
		rightWait = view.findViewById(R.id.right_waiting);

		actionBarFrame = (LinearLayout) view.findViewById(R.id.action_layout);

		((ImageButton) view.findViewById(R.id.nav_button))
				.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						((SingleFragmentContain) getActivity())
								.showNavigation();
					}
				});
	}

	protected void showContinuePlayMediaDialog() {
		new MediaMainActivity.ContinuePlayMediaDialog().show(getActivity()
				.getSupportFragmentManager(), "CONTINUE_PLAY_DIALOG");
	}

	protected void playMedia(List<Uri> uris, String title, Uri uri, int position) {
		((MediaMainActivity) getActivity()).playMedia(uris, title, uri,
				position);
	}

	protected void playMedia(List<Uri> uris, String title) {
		((MediaMainActivity) getActivity()).playMedia(uris, title);
	}

	protected int getGalleryViewHeight() {
		int screenHeight = getActivity().getWindowManager().getDefaultDisplay()
				.getHeight();
		int maxHeight = screenHeight
				- (int) getResources().getDimensionPixelSize(
						R.dimen.custombar_height);

		return Math.min(GALLERY_HEIGHT, maxHeight);
	}

	protected abstract void onMediaPlayResult(Uri uri, int position);

	protected abstract FanartViewStrategy<T> getViewStrategy();

	protected abstract void onSelectPlayMedia(boolean continued);

	protected abstract void playMedia(T media);

	protected abstract int getContentView();

	protected abstract FanartAdapter<T> getFanartAdapter();
}
